const express = require("express")
const router = express.Router()
const {addAdvice, delAdvice,searchAdvice} = require("../control/advice")
const {SuccessModel,ErrorModel} = require("../model/resModel")
const passport = require("passport")


router.post("/add",passport.authenticate("jwt",{session:false}),async (req,res)=>{
  let data =await addAdvice(req,res)
  console.log(data);
  if(data.affectedRows>0){
    res.json(new SuccessModel("添加成功"))
  }else{
    res.json(new ErrorModel("添加失败"))
  }
})
router.post("/del",passport.authenticate("jwt",{session:false}),async (req,res)=>{
  let data =await delAdvice(req,res)
  console.log(data);
  if(data.affectedRows>0){
    res.json(new SuccessModel("删除成功"))
  }else{
    res.json(new ErrorModel("删除失败"))
  }
})
router.get("/search",async (req,res)=>{
  let data =await searchAdvice(req,res)
  if(data.length>0){
    res.json(new SuccessModel(data,"查找成功"))
  }else{
    res.json(new ErrorModel("查找失败"))
  }
})

module.exports=router